{extend name="common/layout" /}

{block name="body"}
<div class="container">
    <!-- 搜索栏 -->
    <form class="layui-form layui-search">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label for="title" class="layui-form-label">标题：</label>
                <div class="layui-input-inline">
                    <input type="text" id="title" name="title" class="layui-input" placeholder="请输入标题名称" autocomplete="off">
                </div>
            </div>
            <div class="layui-inline">
                <label for="status" class="layui-form-label">状态：</label>
                <div class="layui-input-inline">
                    <select id="status" name="status">
                        <option value="" selected>全部</option>
                        <option value="0">显示</option>
                        <option value="1">隐藏</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline layui-input-datetime">
                <label for="datetime" class="layui-form-label">操作时间：</label>
                <div class="layui-input-block">
                    <input type="text" id="datetime" name="datetime" class="layui-input" placeholder="开始时间 - 结束时间" readonly>
                </div>
            </div>
            <div class="layui-inline">
                <a class="layui-btn layui-btn-sm layui-btn-default" lay-submit lay-filter="search">查询</a>
                <a class="layui-btn layui-btn-sm layui-btn-primary" lay-submit lay-filter="clear-search">重置</a>
            </div>
        </div>
    </form>

    <!-- 表格栏 -->
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="wait-table-list" lay-filter="wait-table-list"></table>
            <script type="text/html" id="toolbar">
                <div class="layui-btn-container">
                    <a class="layui-btn layui-btn-sm layui-btn-default {:check_perms('add')}" lay-event="add">
                        <i class="layui-icon icon-add"></i>
                        <span title="w">新增</span>
                    </a>
                    <a class="layui-btn layui-btn-sm layui-btn-danger layui-btn-forbid {:check_perms('del')}" lay-event="leave">
                        <i class="layui-icon icon-del"></i>
                        <span>删除</span>
                    </a>
                </div>
            </script>
            <script type="text/html" id="table-image">
                <div class="table-image-wrap">
                    <img src="{{ d.image }}" alt="img" class="previewImage">
                </div>
            </script>
            <script type="text/html" id="table-topping">
                {{#  if(d.is_topping === 1){ }}
                    <span class="color-success"><i class="layui-icon layui-icon-circle-dot" lay-tips="是"></i></span>
                {{# } else { }}
                    <span class="color-error"><i class="layui-icon layui-icon-circle-dot" lay-tips="否"></i></span>
                {{#  } }}
            </script>
            <script type="text/html" id="table-recommend">
                {{#  if(d.is_recommend === 1){ }}
                    <span class="color-success"><i class="layui-icon layui-icon-circle-dot" lay-tips="是"></i></span>
                {{# } else { }}
                    <span class="color-error"><i class="layui-icon layui-icon-circle-dot" lay-tips="否"></i></span>
                {{#  } }}
            </script>
            <script type="text/html" id="table-show">
                {{#  if(d.is_show === 1){ }}
                    <span class="color-success"><i class="layui-icon layui-icon-circle-dot" lay-tips="显示"></i></span>
                {{# } else { }}
                    <span class="color-warning"><i class="layui-icon layui-icon-circle-dot" lay-tips="隐藏"></i></span>
                {{#  } }}
            </script>
            <script type="text/html" id="table-operate">
                <button type="button" class="layui-btn layui-btn-xs layui-btn-default {:check_perms('edit')}" lay-event="edit">
                    <i class="layui-icon icon-edit"></i>
                </button>
                <button type="button" class="layui-btn layui-btn-xs layui-btn-danger {:check_perms('del')}" lay-event="del">
                    <i class="layui-icon icon-del"></i>
                </button>
            </script>
        </div>
    </div>
</div>
{/block}

{block name="js"}
<script>
    layui.use(['laydate'], function() {
        let laydate = layui.laydate;

        // 时间选择
        laydate.render({elem: '#datetime' ,type: 'datetime' ,trigger: 'click' ,range: true});

        // 渲染表格
        let table = waitUtil.table({
            elem: '#wait-table-list'
            ,url: '{:route("content.article/index")}'
            ,cols: [[
                {type:'checkbox', width:48},
                {field:'id', title:'ID', width:70, align:'center', event:'id'},
                {field:'image', title:'封面', minWidth:60, align:'center', templet:'#table-image'},
                {field:'title', title:'标题', minWidth:300},
                {field:'category', title:'类目', minWidth:100, align:'center'},
                {field:'browse', title:'浏览', minWidth:70, align:'center'},
                {field:'collect', title:'收藏', minWidth:70, align:'center'},
                {field:'sort', title:'排序', minWidth:70, align:'center'},
                {field:'topping', title:'置顶', minWidth:60, align:'center', templet:'#table-topping'},
                {field:'recommend', title:'推荐', minWidth:60, align:'center', templet:'#table-recommend'},
                {field:'show', title:'状态', minWidth:60, align:'center', templet:'#table-show'},
                {field:'create_time', title:'创建时间', minWidth:170, align:'center'},
                {fixed:'right', title:'操作', width:90, align:'center', toolbar:'#table-operate'}
            ]]
        });

        // 逻辑事件
        waitUtil.event({
            add: function () {
                waitUtil.popup({
                    title: '新增',
                    url: '{:route("content.article/add")}',
                    area: ['95%', '95%'],
                    success: function (layero, index) {
                        layero.layui.form.on('submit(addForm)', function(data) {
                            waitUtil.locking(this);
                            data.field['content'] = layero.tinymce.activeEditor.getContent();
                            waitUtil.ajax({
                                url: '{:route("content.article/add")}',
                                type: 'POST',
                                data: data.field
                            }).then((res) => {
                                waitUtil.unlock(this);
                                if (res.code === 0) {
                                    table.reload({page: {curr: 1}});
                                    layer.close(index);
                                }
                            }).catch(() => {
                                waitUtil.unlock(this);
                            });
                        });
                    }
                });
            },
            edit: function (obj) {
                waitUtil.popup({
                    title: '编辑',
                    url: '{:route("content.article/edit")}?id='+obj.data.id,
                    area: ['95%', '95%'],
                    success: function (layero, index) {
                        layero.layui.form.on('submit(addForm)', function(data) {
                            waitUtil.locking(this);
                            data.field['id'] = obj.data.id;
                            data.field['content'] = layero.tinymce.activeEditor.getContent();
                            waitUtil.ajax({
                                url: '{:route("content.article/edit")}',
                                type: 'POST',
                                data: data.field
                            }).then((res) => {
                                waitUtil.unlock(this);
                                if (res.code === 0) {
                                    table.reload();
                                    layer.close(index);
                                }
                            }).catch((e) => {
                                console.log(e)
                                waitUtil.unlock(this);
                            });
                        });
                    }
                });
            },
            del: function (obj) {
                layer.confirm('确定要删除此项数据吗？', function(index) {
                    layer.close(index);
                    waitUtil.ajax({
                        url: '{:route("content.article/del")}',
                        type: 'POST',
                        data: {ids: [obj.data.id]}
                    }).then((res) => {
                        if (res.code === 0) {
                            table.reload();
                        }
                    });
                });
            },
            leave: function () {
                let ids = waitUtil.checkbox();
                if (!ids.length) {
                    return layer.msg('请至少选择一项！', {icon: 2});
                }
                layer.confirm(`确定要删除选中的${ids.length}项数据吗？`, function(index) {
                    layer.close(index);
                    waitUtil.ajax({
                        url: '{:route("content.article/del")}',
                        type: 'POST',
                        data: {ids: ids}
                    }).then((res) => {
                        if (res.code === 0) {
                            table.reload();
                        }
                    });
                });
            }
        });

        // 搜索事件
        waitUtil.search(table);
    });
</script>
{/block}